import { FMVCS } from "../../fgui/fmvc/FMVCS";
import MapLocalData from "./MapLocalData";
import { GlobalEvent } from "../global/Global";
import Manager from "../../manager/Manager";
import { ConstDefine } from "../common/ConstDefine";
import MapLocalView from "./MapLocalView";

export default class MapLocalCtrl extends FMVCS.FCtrl {
    /** 数据实体 */
    static data: MapLocalData = null;

    constructor() {
        super();
        MapLocalCtrl.data = new MapLocalData();
    }

    /** 事件监听注册 */
    rigisterEventListener(){
        this.on(GlobalEvent.UIListenID.UI_MapLocalView_Open, this.onOpenView, this);
        this.on(GlobalEvent.UIListenID.UI_MapSaveToLocal, this.onSaveToLocal, this);
        this.on(GlobalEvent.UIListenID.UI_CustomMapNext, this.onCustomMapNext, this);
    }

    onOpenView(){
        Manager.fui.open(ConstDefine.UI.MapLocalView, MapLocalView);
    }

    onSaveToLocal(map: ConstDefine.Config.StarConfig, pattern: ConstDefine.GameMain.Pattern){
        if(pattern == ConstDefine.GameMain.Pattern.create)
            MapLocalCtrl.data.addMap(map);
        else
            MapLocalCtrl.data.rivisionsMap(map);
    }

    onCustomMapNext(){
        MapLocalCtrl.data.vo.playMapIndex++;
        MapLocalCtrl.onPlayCustomMap(MapLocalCtrl.data.vo.playMapIndex);
    }

    static onPlayCustomMap(mapIndex: number){
        let map = this.data.vo.mapList[mapIndex];
        if(map){
            this.data.vo.playMapIndex = mapIndex;
            this.emit(GlobalEvent.GameEventID.GameCustomMap, map);
        }
        else{
            Manager.fui.open(ConstDefine.UI.MapLocalView, MapLocalView);
            this.emit(GlobalEvent.UIListenID.UI_ShowTips, `没有下一关地图了!`);
        }
    }
}
